home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS / oxygen / samples / docbook / xsl / common / labels.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2002-09-09  |  18.9 KB  |  555 lines  |  [□□□□/□□□□]

  1. labels.xslI-π¢tÚπ¢tÚÅÅÎ8<?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.                 xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
  4.                 exclude-result-prefixes="doc"
  5.                 version='1.0'>
  6.  
  7. <!-- ============================================================ -->
  8. <!-- label markup -->
  9.  
  10. <doc:mode mode="label.markup" xmlns="">
  11. <refpurpose>Provides access to element labels</refpurpose>
  12. <refdescription>
  13. <para>Processing an element in the
  14. <literal role="mode">label.markup</literal> mode produces the
  15. element label.</para>
  16. <para>Trailing punctuation is not added to the label.
  17. </para>
  18. </refdescription>
  19. </doc:mode>
  20.  
  21. <xsl:template match="*" mode="intralabel.punctuation">
  22.   <xsl:text>.</xsl:text>
  23. </xsl:template>
  24.  
  25. <xsl:template match="*" mode="label.markup">
  26.   <xsl:message>
  27.     <xsl:text>Request for label of unexpected element: </xsl:text>
  28.     <xsl:value-of select="name(.)"/>
  29.   </xsl:message>
  30. </xsl:template>
  31.  
  32. <xsl:template match="set|book" mode="label.markup">
  33.   <xsl:if test="@label">
  34.     <xsl:value-of select="@label"/>
  35.   </xsl:if>
  36. </xsl:template>
  37.  
  38. <xsl:template match="part" mode="label.markup">
  39.   <xsl:choose>
  40.     <xsl:when test="@label">
  41.       <xsl:value-of select="@label"/>
  42.     </xsl:when>
  43.     <xsl:when test="$part.autolabel != 0">
  44.       <xsl:number from="book" count="part" format="I"/>
  45.     </xsl:when>
  46.   </xsl:choose>
  47. </xsl:template>
  48.  
  49. <xsl:template match="partintro" mode="label.markup">
  50.   <!-- no label -->
  51. </xsl:template>
  52.  
  53. <xsl:template match="preface" mode="label.markup">
  54.   <xsl:choose>
  55.     <xsl:when test="@label">
  56.       <xsl:value-of select="@label"/>
  57.     </xsl:when>
  58.     <xsl:when test="$preface.autolabel != 0">
  59.       <xsl:choose>
  60.         <xsl:when test="$label.from.part != 0 and ancestor::part">
  61.           <xsl:number from="part" count="preface" format="1" level="any"/>
  62.         </xsl:when>
  63.         <xsl:otherwise>
  64.           <xsl:number from="book" count="preface" format="1" level="any"/>
  65.         </xsl:otherwise>
  66.       </xsl:choose>
  67.     </xsl:when>
  68.   </xsl:choose>
  69. </xsl:template>
  70.  
  71. <xsl:template match="chapter" mode="label.markup">
  72.   <xsl:choose>
  73.     <xsl:when test="@label">
  74.       <xsl:value-of select="@label"/>
  75.     </xsl:when>
  76.     <xsl:when test="$chapter.autolabel != 0">
  77.       <xsl:choose>
  78.         <xsl:when test="$label.from.part != 0 and ancestor::part">
  79.           <xsl:number from="part" count="chapter" format="1" level="any"/>
  80.         </xsl:when>
  81.         <xsl:otherwise>
  82.           <xsl:number from="book" count="chapter" format="1" level="any"/>
  83.         </xsl:otherwise>
  84.       </xsl:choose>
  85.     </xsl:when>
  86.   </xsl:choose>
  87. </xsl:template>
  88.  
  89. <xsl:template match="appendix" mode="label.markup">
  90.   <xsl:choose>
  91.     <xsl:when test="@label">
  92.       <xsl:value-of select="@label"/>
  93.     </xsl:when>
  94.     <xsl:when test="$appendix.autolabel != 0">
  95.       <xsl:choose>
  96.         <xsl:when test="$label.from.part != 0 and ancestor::part">
  97.           <xsl:number from="part" count="appendix" format="A" level="any"/>
  98.         </xsl:when>
  99.         <xsl:otherwise>
  100.           <xsl:number from="book|article"
  101.                       count="appendix" format="A" level="any"/>
  102.         </xsl:otherwise>
  103.       </xsl:choose>
  104.     </xsl:when>
  105.   </xsl:choose>
  106. </xsl:template>
  107.  
  108. <xsl:template match="article" mode="label.markup">
  109.   <xsl:if test="@label">
  110.     <xsl:value-of select="@label"/>
  111.   </xsl:if>
  112. </xsl:template>
  113.  
  114. <xsl:template match="dedication|colophon" mode="label.markup">
  115.   <xsl:if test="@label">
  116.     <xsl:value-of select="@label"/>
  117.   </xsl:if>
  118. </xsl:template>
  119.  
  120. <xsl:template match="reference" mode="label.markup">
  121.   <xsl:choose>
  122.     <xsl:when test="@label">
  123.       <xsl:value-of select="@label"/>
  124.     </xsl:when>
  125.     <xsl:when test="$part.autolabel != 0">
  126.       <xsl:number from="book" count="reference" format="I" level="any"/>
  127.     </xsl:when>
  128.   </xsl:choose>
  129. </xsl:template>
  130.  
  131. <xsl:template match="refentry" mode="label.markup">
  132.   <xsl:if test="@label">
  133.     <xsl:value-of select="@label"/>
  134.   </xsl:if>
  135. </xsl:template>
  136.  
  137. <xsl:template match="section" mode="label.markup">
  138.   <!-- if this is a nested section, label the parent -->
  139.   <xsl:if test="local-name(..) = 'section'">
  140.     <xsl:variable name="parent.section.label">
  141.       <xsl:apply-templates select=".." mode="label.markup"/>
  142.     </xsl:variable>
  143.     <xsl:if test="$parent.section.label != ''">
  144.       <xsl:apply-templates select=".." mode="label.markup"/>
  145.       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  146.     </xsl:if>
  147.   </xsl:if>
  148.  
  149.   <!-- if the parent is a component, maybe label that too -->
  150.   <xsl:variable name="parent.is.component">
  151.     <xsl:call-template name="is.component">
  152.       <xsl:with-param name="node" select=".."/>
  153.     </xsl:call-template>
  154.   </xsl:variable>
  155.  
  156.   <!-- does this section get labelled? -->
  157.   <xsl:variable name="label">
  158.     <xsl:call-template name="label.this.section">
  159.       <xsl:with-param name="section" select="."/>
  160.     </xsl:call-template>
  161.   </xsl:variable>
  162.  
  163.   <xsl:if test="$section.label.includes.component.label != 0
  164.                 and $parent.is.component != 0">
  165.     <xsl:variable name="parent.label">
  166.       <xsl:apply-templates select=".." mode="label.markup"/>
  167.     </xsl:variable>
  168.     <xsl:if test="$parent.label != ''">
  169.       <xsl:apply-templates select=".." mode="label.markup"/>
  170.       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  171.     </xsl:if>
  172.   </xsl:if>
  173.  
  174. <!--
  175.   <xsl:message>
  176.     <xsl:value-of select="$label"/>, <xsl:number count="section"/>
  177.   </xsl:message>
  178. -->
  179.  
  180.   <xsl:choose>
  181.     <xsl:when test="@label">
  182.       <xsl:value-of select="@label"/>
  183.     </xsl:when>
  184.     <xsl:when test="$label != 0">
  185.       <xsl:number count="section"/>
  186.     </xsl:when>
  187.   </xsl:choose>
  188. </xsl:template>
  189.  
  190. <xsl:template match="sect1" mode="label.markup">
  191.   <!-- if the parent is a component, maybe label that too -->
  192.   <xsl:variable name="parent.is.component">
  193.     <xsl:call-template name="is.component">
  194.       <xsl:with-param name="node" select=".."/>
  195.     </xsl:call-template>
  196.   </xsl:variable>
  197.  
  198.   <xsl:if test="$section.label.includes.component.label != 0
  199.                 and $parent.is.component">
  200.     <xsl:variable name="parent.label">
  201.       <xsl:apply-templates select=".." mode="label.markup"/>
  202.     </xsl:variable>
  203.     <xsl:if test="$parent.label != ''">
  204.       <xsl:apply-templates select=".." mode="label.markup"/>
  205.       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  206.     </xsl:if>
  207.   </xsl:if>
  208.  
  209.   <xsl:choose>
  210.     <xsl:when test="@label">
  211.       <xsl:value-of select="@label"/>
  212.     </xsl:when>
  213.     <xsl:when test="$section.autolabel != 0">
  214.       <xsl:number count="sect1"/>
  215.     </xsl:when>
  216.   </xsl:choose>
  217. </xsl:template>
  218.  
  219. <xsl:template match="sect2|sect3|sect4|sect5" mode="label.markup">
  220.   <!-- label the parent -->
  221.   <xsl:variable name="parent.label">
  222.     <xsl:apply-templates select=".." mode="label.markup"/>
  223.   </xsl:variable>
  224.   <xsl:if test="$parent.label != ''">
  225.     <xsl:apply-templates select=".." mode="label.markup"/>
  226.     <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  227.   </xsl:if>
  228.  
  229.   <xsl:choose>
  230.     <xsl:when test="@label">
  231.       <xsl:value-of select="@label"/>
  232.     </xsl:when>
  233.     <xsl:when test="$section.autolabel != 0">
  234.       <xsl:choose>
  235.         <xsl:when test="local-name(.) = 'sect2'">
  236.       <xsl:number count="sect2"/>
  237.     </xsl:when>
  238.     <xsl:when test="local-name(.) = 'sect3'">
  239.       <xsl:number count="sect3"/>
  240.     </xsl:when>
  241.     <xsl:when test="local-name(.) = 'sect4'">
  242.       <xsl:number count="sect4"/>
  243.     </xsl:when>
  244.     <xsl:when test="local-name(.) = 'sect5'">
  245.       <xsl:number count="sect5"/>
  246.     </xsl:when>
  247.     <xsl:otherwise>
  248.       <xsl:message>label.markup: this can't happen!</xsl:message>
  249.     </xsl:otherwise>
  250.       </xsl:choose>
  251.     </xsl:when>
  252.   </xsl:choose>
  253. </xsl:template>
  254.  
  255. <xsl:template match="refsect1" mode="label.markup">
  256.   <xsl:choose>
  257.     <xsl:when test="@label">
  258.       <xsl:value-of select="@label"/>
  259.     </xsl:when>
  260.     <xsl:when test="$section.autolabel != 0">
  261.       <xsl:number count="refsect1"/>
  262.     </xsl:when>
  263.   </xsl:choose>
  264. </xsl:template>
  265.  
  266. <xsl:template match="refsect2|refsect3" mode="label.markup">
  267.   <!-- label the parent -->
  268.   <xsl:variable name="parent.label">
  269.     <xsl:apply-templates select=".." mode="label.markup"/>
  270.   </xsl:variable>
  271.   <xsl:if test="$parent.label != ''">
  272.     <xsl:apply-templates select=".." mode="label.markup"/>
  273.     <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  274.   </xsl:if>
  275.  
  276.   <xsl:choose>
  277.     <xsl:when test="@label">
  278.       <xsl:value-of select="@label"/>
  279.     </xsl:when>
  280.     <xsl:when test="$section.autolabel != 0">
  281.       <xsl:choose>
  282.         <xsl:when test="local-name(.) = 'refsect2'">
  283.       <xsl:number count="refsect2"/>
  284.     </xsl:when>
  285.         <xsl:otherwise>
  286.       <xsl:number count="refsect3"/>
  287.     </xsl:otherwise>
  288.       </xsl:choose>
  289.     </xsl:when>
  290.   </xsl:choose>
  291. </xsl:template>
  292.  
  293. <xsl:template match="simplesect" mode="label.markup">
  294.   <!-- if this is a nested section, label the parent -->
  295.   <xsl:if test="local-name(..) = 'section'
  296.                 or local-name(..) = 'sect1'
  297.                 or local-name(..) = 'sect2'
  298.                 or local-name(..) = 'sect3'
  299.                 or local-name(..) = 'sect4'
  300.                 or local-name(..) = 'sect5'">
  301.     <xsl:variable name="parent.section.label">
  302.       <xsl:apply-templates select=".." mode="label.markup"/>
  303.     </xsl:variable>
  304.     <xsl:if test="$parent.section.label != ''">
  305.       <xsl:apply-templates select=".." mode="label.markup"/>
  306.       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  307.     </xsl:if>
  308.   </xsl:if>
  309.  
  310.   <!-- if the parent is a component, maybe label that too -->
  311.   <xsl:variable name="parent.is.component">
  312.     <xsl:call-template name="is.component">
  313.       <xsl:with-param name="node" select=".."/>
  314.     </xsl:call-template>
  315.   </xsl:variable>
  316.  
  317.   <!-- does this section get labelled? -->
  318.   <xsl:variable name="label">
  319.     <xsl:call-template name="label.this.section">
  320.       <xsl:with-param name="section" select="."/>
  321.     </xsl:call-template>
  322.   </xsl:variable>
  323.  
  324.   <xsl:if test="$section.label.includes.component.label != 0
  325.                 and $parent.is.component != 0">
  326.     <xsl:variable name="parent.label">
  327.       <xsl:apply-templates select=".." mode="label.markup"/>
  328.     </xsl:variable>
  329.     <xsl:if test="$parent.label != ''">
  330.       <xsl:apply-templates select=".." mode="label.markup"/>
  331.       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
  332.     </xsl:if>
  333.   </xsl:if>
  334.  
  335.   <xsl:choose>
  336.     <xsl:when test="@label">
  337.       <xsl:value-of select="@label"/>
  338.     </xsl:when>
  339.     <xsl:when test="$label != 0">
  340.       <xsl:number count="simplesect"/>
  341.     </xsl:when>
  342.   </xsl:choose>
  343. </xsl:template>
  344.  
  345. <xsl:template match="qandadiv" mode="label.markup">
  346.   <xsl:variable name="lparent" select="(ancestor::set
  347.                                        |ancestor::book
  348.                                        |ancestor::chapter
  349.                                        |ancestor::appendix
  350.                                        |ancestor::preface
  351.                                        |ancestor::section
  352.                                        |ancestor::simplesect
  353.                                        |ancestor::sect1
  354.                                        |ancestor::sect2
  355.                                        |ancestor::sect3
  356.                                        |ancestor::sect4
  357.                                        |ancestor::sect5
  358.                                        |ancestor::refsect1
  359.                                        |ancestor::refsect2
  360.                                        |ancestor::refsect3)[last()]"/>
  361.  
  362.   <xsl:variable name="lparent.prefix">
  363.     <xsl:apply-templates select="$lparent" mode="label.markup"/>
  364.   </xsl:variable>
  365.  
  366.   <xsl:variable name="prefix">
  367.     <xsl:if test="$qanda.inherit.numeration != 0">
  368.       <xsl:if test="$lparent.prefix != ''">
  369.         <xsl:apply-templates select="$lparent" mode="label.markup"/>
  370.         <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
  371.       </xsl:if>
  372.     </xsl:if>
  373.   </xsl:variable>
  374.  
  375.   <xsl:choose>
  376.     <xsl:when test="@label">
  377.       <xsl:value-of select="$prefix"/>
  378.       <xsl:value-of select="@label"/>
  379.     </xsl:when>
  380.     <xsl:when test="$qandadiv.autolabel != 0">
  381.       <xsl:value-of select="$prefix"/>
  382.       <xsl:number level="multiple" count="qandadiv" format="1"/>
  383.     </xsl:when>
  384.   </xsl:choose>
  385. </xsl:template>
  386.  
  387. <xsl:template match="question|answer" mode="label.markup">
  388.   <xsl:variable name="lparent" select="(ancestor::set
  389.                                        |ancestor::book
  390.                                        |ancestor::chapter
  391.                                        |ancestor::appendix
  392.                                        |ancestor::preface
  393.                                        |ancestor::section
  394.                                        |ancestor::simplesect
  395.                                        |ancestor::sect1
  396.                                        |ancestor::sect2
  397.                                        |ancestor::sect3
  398.                                        |ancestor::sect4
  399.                                        |ancestor::sect5
  400.                                        |ancestor::refsect1
  401.                                        |ancestor::refsect2
  402.                                        |ancestor::refsect3)[last()]"/>
  403.  
  404.   <xsl:variable name="lparent.prefix">
  405.     <xsl:apply-templates select="$lparent" mode="label.markup"/>
  406.   </xsl:variable>
  407.  
  408.   <xsl:variable name="prefix">
  409.     <xsl:if test="$qanda.inherit.numeration != 0">
  410.       <xsl:if test="$lparent.prefix != ''">
  411.         <xsl:apply-templates select="$lparent" mode="label.markup"/>
  412.         <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
  413.       </xsl:if>
  414.       <xsl:if test="ancestor::qandadiv">
  415.         <xsl:apply-templates select="ancestor::qandadiv[1]" mode="label.markup"/>
  416.         <xsl:apply-templates select="ancestor::qandadiv[1]"
  417.                              mode="intralabel.punctuation"/>
  418.       </xsl:if>
  419.     </xsl:if>
  420.   </xsl:variable>
  421.  
  422.   <xsl:variable name="inhlabel"
  423.                 select="ancestor-or-self::qandaset/@defaultlabel[1]"/>
  424.  
  425.   <xsl:variable name="deflabel">
  426.     <xsl:choose>
  427.       <xsl:when test="$inhlabel != ''">
  428.         <xsl:value-of select="$inhlabel"/>
  429.       </xsl:when>
  430.       <xsl:otherwise>
  431.         <xsl:value-of select="$qanda.defaultlabel"/>
  432.       </xsl:otherwise>
  433.     </xsl:choose>
  434.   </xsl:variable>
  435.  
  436.   <xsl:variable name="label" select="label"/>
  437.  
  438.   <xsl:choose>
  439.     <xsl:when test="count($label)>0">
  440.       <xsl:apply-templates select="$label"/>
  441.     </xsl:when>
  442.  
  443.     <xsl:when test="$deflabel = 'qanda' and local-name(.) = 'question'">
  444.       <xsl:call-template name="gentext">
  445.         <xsl:with-param name="key" select="'Question'"/>
  446.       </xsl:call-template>
  447.     </xsl:when>
  448.  
  449.     <xsl:when test="$deflabel = 'qanda' and local-name(.) = 'answer'">
  450.       <xsl:call-template name="gentext">
  451.         <xsl:with-param name="key" select="'Answer'"/>
  452.       </xsl:call-template>
  453.     </xsl:when>
  454.  
  455.     <xsl:when test="$deflabel = 'number' and local-name(.) = 'question'">
  456.       <xsl:value-of select="$prefix"/>
  457.       <xsl:number level="multiple" count="qandaentry" format="1"/>
  458.       <xsl:text>. </xsl:text>
  459.     </xsl:when>
  460.   </xsl:choose>
  461. </xsl:template>
  462.  
  463. <xsl:template match="bibliography|glossary|index" mode="label.markup">
  464.   <xsl:if test="@label">
  465.     <xsl:value-of select="@label"/>
  466.   </xsl:if>
  467. </xsl:template>
  468.  
  469. <xsl:template match="figure|table|example|equation|procedure"
  470.               mode="label.markup">
  471.   <xsl:variable name="pchap"
  472.                 select="ancestor::chapter
  473.                         |ancestor::appendix
  474.                         |ancestor::article[ancestor::book]"/>
  475.  
  476.   <xsl:variable name="prefix">
  477.     <xsl:if test="count($pchap) > 0">
  478.       <xsl:apply-templates select="$pchap" mode="label.markup"/>
  479.     </xsl:if>
  480.   </xsl:variable>
  481.  
  482.   <xsl:choose>
  483.     <xsl:when test="@label">
  484.       <xsl:value-of select="@label"/>
  485.     </xsl:when>
  486.     <xsl:otherwise>
  487.       <xsl:choose>
  488.         <xsl:when test="count($pchap)>0">
  489.           <xsl:if test="$prefix != ''">
  490.             <xsl:apply-templates select="$pchap" mode="label.markup"/>
  491.             <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
  492.           </xsl:if>
  493.           <xsl:number format="1" from="chapter|appendix" level="any"/>
  494.         </xsl:when>
  495.         <xsl:otherwise>
  496.           <xsl:number format="1" from="book|article" level="any"/>
  497.         </xsl:otherwise>
  498.       </xsl:choose>
  499.     </xsl:otherwise>
  500.   </xsl:choose>
  501. </xsl:template>
  502.  
  503. <xsl:template match="orderedlist/listitem" mode="label.markup">
  504.   <xsl:variable name="numeration">
  505.     <xsl:call-template name="list.numeration">
  506.       <xsl:with-param name="node" select="parent::orderedlist"/>
  507.     </xsl:call-template>
  508.   </xsl:variable>
  509.  
  510.   <xsl:variable name="type">
  511.     <xsl:choose>
  512.       <xsl:when test="$numeration='arabic'">1</xsl:when>
  513.       <xsl:when test="$numeration='loweralpha'">a</xsl:when>
  514.       <xsl:when test="$numeration='lowerroman'">i</xsl:when>
  515.       <xsl:when test="$numeration='upperalpha'">A</xsl:when>
  516.       <xsl:when test="$numeration='upperroman'">I</xsl:when>
  517.       <!-- What!? This should never happen -->
  518.       <xsl:otherwise>
  519.         <xsl:message>
  520.           <xsl:text>Unexpected numeration: </xsl:text>
  521.           <xsl:value-of select="$numeration"/>
  522.         </xsl:message>
  523.         <xsl:value-of select="1."/>
  524.       </xsl:otherwise>
  525.     </xsl:choose>
  526.   </xsl:variable>
  527.  
  528.   <xsl:number count="listitem" format="{$type}"/>
  529. </xsl:template>
  530.  
  531. <xsl:template match="abstract" mode="label.markup">
  532.   <!-- nop -->
  533. </xsl:template>
  534.  
  535. <!-- ============================================================ -->
  536.  
  537. <xsl:template name="label.this.section">
  538.   <xsl:param name="section" select="."/>
  539.   <xsl:value-of select="$section.autolabel"/>
  540. </xsl:template>
  541.  
  542. <doc:template name="label.this.section" xmlns="">
  543. <refpurpose>Returns true if $section should be labelled</refpurpose>
  544. <refdescription>
  545. <para>Returns true if the specified section should be labelled.
  546. By default, this template simply returns $section.autolabel, but
  547. custom stylesheets may override it to get more selective behavior.</para>
  548. </refdescription>
  549. </doc:template>
  550.  
  551. <!-- ============================================================ -->
  552.  
  553. </xsl:stylesheet>
  554. This resource fork intentionally left blank   ˇˇ